import {defineStore} from 'pinia'
import {ref} from 'vue'
import {login} from '../api/user'
import { deleteUser } from '../api/user'

const useUserStore = defineStore('user',()=>{
    const token = ref(null)
    const userInfo = ref(null)

  /**
   * 处理用户登录，更新状态数据
   * @param user 
   */
   async function loginAction(user) {
    const result = await login(user)
    console.log('result:', result)
    // 结果中有 userInfo 则说明登录成功，则否登录失败
    if (result.data) {
      token.value = result.data.token
      userInfo.value = result.data.userInfo
      return true // 成功
    } else {
      return false // 失败
    }
  }

  return {
    token,
    userInfo,
    loginAction,
  }
}, 
{
  persist: {
    storage: sessionStorage,
    paths: ['token']
  },
},)

export default useUserStore


